package base.tag;

import java.util.ArrayList;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;

public class BsColumnTag extends BsContainerTag<BsColumnModel> {

	private static final long serialVersionUID = 1L;
	
	protected int level = 1;
	protected int colspan = 0;
	protected int rowspan = 0;

	public BsColumnTag() {
		items = new ArrayList<BsColumnModel>();
	}
	
	@Override
	public int doStartTag() throws JspException {
		
		items.clear();
		this.colspan = 0;
		return Tag.EVAL_PAGE;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public int doEndTag() throws JspException {

		BsContainerTag parent = (BsContainerTag)this.getParent();

		BsColumnTag column = null;
		if(this.getParent().getClass().getName().compareTo("base.tag.BsColumnTag") == 0)
			column = (BsColumnTag)this.getParent();
		
		BsColumnModel model = new BsColumnModel();
		model.setCaption(caption);
		model.setField(name);
        model.setWidth(width);
		if (this.items.size() == 0)
		{
			this.colspan = 1;
			if (column != null)
				column.colspan += this.colspan;
		}
		if (this.items.size() > 0)
		{
			if (column != null)
				column.colspan += this.colspan;

			for(int i = 0; i < items.size(); i++)
				model.getItems().add((BsColumnModel) items.get(i));
		}
		if(column != null)
			this.level = column.level + 1;
		else
			this.level = 1;
        model.setLevel(level);
		model.setColspan(this.colspan);
		parent.items.add(model);
		return Tag.EVAL_PAGE;
	}

}
